package com.woniu.p2p.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.service.InvestService;
import com.woniu.p2p.vo.InvestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/invest")
public class InvestController {

    @Autowired
    private InvestService investService;

    @RequestMapping("searchAll")
    public PageInfo<InvestVo> searchAll(@RequestBody  Map condition){

        Object pageNumS = condition.get("pageNum");
        int pageNum = pageNumS == null ? 1 : (int)pageNumS;
        Map<String,Object> orders = (Map<String,Object>) condition.get("orders");

        //由于动态sql不利于判断是否所有如果所有的排序条件为空,设orders为空
        if(orders!=null && orders.size()!=0){
            Boolean flag = true;
            for (String order:orders.keySet()) {
                Object value = orders.get(order);
                if(value!=null && !"".equals(value)){
                    flag = false;
                    break;
                };
            }
            if (flag){
                condition.replace("orders",null);
            }
        }else {
            condition.replace("orders", null);
        }
        PageHelper.startPage(pageNum,10);
        PageInfo<InvestVo> pageInfo = new PageInfo<>(investService.searchAll(condition));
        return pageInfo;
    }
}
